package com.ys.shardingjdbcdemo.goods.sharding;

import org.apache.shardingsphere.api.sharding.standard.PreciseShardingAlgorithm;
import org.apache.shardingsphere.api.sharding.standard.PreciseShardingValue;
import org.springframework.stereotype.Component;

import java.util.Collection;

/**
 * @program: sharding-jdbc-demo
 * @description: 分库路由算法
 * @author: Henry
 * @create 2020-06-07 20:19
 **/
@Component
public class PreciseShardingDatabaseAlgorithm implements PreciseShardingAlgorithm<Long> {
    @Override
    public String doSharding(Collection<String> collection, PreciseShardingValue<Long> preciseShardingValue) {
        String[] databases = collection.toArray(new String[0]);
        int index = (int)((preciseShardingValue.getValue() - 1) % databases.length);
        return databases[index];
    }
}
